﻿using DNFImagePacks2Manager.Models;
using DNFImagePacks2Manager.NPK;
using Microsoft.UI.Xaml.Controls;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace DNFImagePacks2Manager.QuickTool.Models
{
    public class QuickToolImgReplace : QuickToolBase
    {
        public List<ReplaceImageIndex> imgList = [];

        public QuickToolImgReplace(string name, string desc) : base(name, desc)
        {
        }

        public override Dictionary<string, string> GetInfo()
        {
            var infos =  base.GetInfo();
            infos.Add("涉及IMG数量", $"{imgList.Count()}个");
            int i = 1;
            foreach (var img in imgList)
            {
                infos.Add($"{i++}", $"{(img.ReadOnly?"[只读]":"")}{img.Name}");
            }
            return infos;
        }

        public List<ReplaceImage> LoadImage()
        {
            List<ReplaceImage> riList = [];
            foreach (var item in imgList)
            {
                string imgFullName = item.ImgName;
                var imageList = item.ImageList;
                string? originalNpkName = Path.GetDirectoryName(imgFullName)?.Replace(Path.DirectorySeparatorChar, '_') + ".NPK";
                if (originalNpkName != null)
                {
                    NPKItemImageOfficial npk = new(originalNpkName);
                    NPKIndex? imgIndex = NPKFile.ReadIMGFromNPK(npk, imgFullName);
                    if (imgIndex != null)
                    {
                        IMGFile? imgFile = imgIndex.GetIMGFile();
                        if (imgFile != null)
                        {
                            if (item.ConvertV2 && imgFile is IConvertIMGV2 convertImg)
                            {
                                imgFile = convertImg.Convert2IMGV2File();
                                imgIndex.SetIMGFile(imgFile);
                            }
                            item.ImageIndex = imgIndex;
                            if(imageList.Count == 1)
                            {
                                var image = imageList[0];
                                if (image.AllPointer)
                                {
                                    int index = image.Index;
                                    var pointerFile = new ImagePointerFile(index);
                                    int imageCount = imgFile.Images.Count;
                                    for (int i = 0; i < imageCount; i++)
                                    {
                                        if (i != index)
                                        {
                                            imgFile.SetImage(i, pointerFile);
                                        }
                                    }
                                }
                                else if (image.IsAnimate && image.SubImage.Count == 0 && image.Index == 0)
                                {
                                    string prefix = image.Name;
                                    int imageCount = imgFile.Images.Count;
                                    for (int i = 1; i < imageCount; i++)
                                    {
                                        string name = $"{prefix} - {i}";
                                        ReplaceImage ri = new()
                                        {
                                            Index = i,
                                            Name = name,
                                            IsDisplay = false
                                        };
                                        imageList.Add(ri);
                                        image.SubImage.Add(name);
                                    }
                                }
                            }
                            foreach (var image in imageList)
                            {
                                if (image.Pointer > -1)
                                {
                                    int pointer = image.Pointer;
                                    var pointerFile = new ImagePointerFile(pointer);
                                    imgFile.SetImage(image.Index, pointerFile);
                                }
                                else if (image.Assets == null)
                                {
                                    var bitmap = imgFile.GetPreviewImage(image.Index);
                                    if (bitmap != null)
                                    {
                                        image.SetPreviewImage(bitmap);
                                    }
                                }
                                else
                                {
                                    image.SetAssetsImage();
                                    image.SetPreviewImage();
                                }
                            }
                        }
                    }
                    riList.AddRange(imageList);
                }
            }
            return riList;
        }

        public override void OpenUsePage(Page page)
        {
            App.GetMainWindow()?.ChangeAndInitializePage("ImgReplace", this);
        }
    }
}
